package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QiniuUtils;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.xmlbeans.impl.jam.mutable.MElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-21 16:55
 **/
@RestController
@RequestMapping("setmeal")
public class SeteamlController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("queryCheckGroup")
    public Result queryCheckGroup(){
        List<CheckGroup> list = setmealService.queryCheckGroup();
        return new Result(true, "查询成功", list);
    }

    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile){
        //产生一个独一无二不重复唯一的名字,并且加上后缀
        //想想怎么解决
        //获取后缀
        try {
            String filename = imgFile.getOriginalFilename();
            int index = filename.indexOf(".");
            String suffix = filename.substring(index);
            //生成一个随机数并拼接后缀
            filename = UUID.randomUUID() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), filename);
            //在每次上传图片至七牛云时将图片名称保存至redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, filename);
            Set<String> diffImg = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_DB_RESOURCES, RedisConstant.SETMEAL_PIC_RESOURCES);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filename);
        } catch (IOException e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    @RequestMapping("add")
    public Result add(@RequestParam("checkgroupIds")Integer[] checkgroupIds, @RequestBody Setmeal setmeal){

        try {
            setmealService.add(checkgroupIds, setmeal);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("findPage")
    //json格式数据要加requestbody
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            PageResult pageResult = setmealService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }




}
